package com.chen.deskclock.ScheduleManagement;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.TimePicker;

import com.chen.deskclock.R;

import java.time.LocalDate;
import java.time.Period;
import java.util.Date;

public class StartPage2 extends AppCompatActivity {

    private Button btn1;
    private Button btn2;
    private TimePicker timePicker;
    private TextView sleep_time;
    private Date date =new Date();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_page2);
        timePicker=findViewById(R.id.time_picker);
        timePicker.setIs24HourView(true);
        timePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
        timePicker.setCurrentHour(6); //当前小时
        timePicker.setCurrentMinute(0); //当前分钟
        date.setHours(6);
        date.setMinutes(0);
        btn1=findViewById(R.id.back_step);
        btn2=findViewById(R.id.continue_step);
        sleep_time=findViewById(R.id.sleep_time);
        //sleep_time.setText(R.string.sleep_time+R.string.hours_label);
        Intent intent = getIntent();
        //获取传递的值
        Integer hour_1= intent.getIntExtra("Hour",22);
        Integer minute_1=intent.getIntExtra("Minute",0);
        Date date0=new Date();
        date0.setHours(hour_1);
        date0.setMinutes(minute_1);
        sleep_time.setText("睡眠时长"+diff(date0,date));
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(StartPage2.this, StartPage3.class);
                startActivity(intent);
            }
        });
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int i, int i1) {
                date.setHours(i);
                date.setMinutes(i1);
                sleep_time.setText("睡眠时长"+diff(date0,date));
            }
        });
    }
    private String diff(Date start, Date end) {
        Long diff = 1000 * 60 * 60 * 24+end.getTime()+60*1000 - start.getTime(); //两时间差，精确到毫秒
        Long day = diff / (1000 * 60 * 60 * 24); //以天数为单位取整
        Long hour=(diff/(60*60*1000)-day*24); //以小时为单位取整
        Long minutes=((diff/(60*1000))-day*24*60-hour*60); //以分钟为单位取整
        Long second=(diff/1000-day*24*60*60-hour*60*60-minutes*60);//秒
        if (minutes==0){
            return String.valueOf(hour)+"小时";
        }else {
            return String.valueOf(hour)+"小时"+String.valueOf(minutes)+"分钟";
        }

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return false;
    }

}